package com.jrx.anytxn.param.service.account.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.dto.account.SubjectSummaryInfoRes;
import com.jrx.anytxn.param.entity.GaSubjectSummaryInfo;
import com.jrx.anytxn.param.mapper.ext.ExtGaSubjectSummaryInfoMapper;
import com.jrx.anytxn.param.service.account.ISubjectSummaryInfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 科目总账表 的接口实现类
 *
 * @author Qingyao Xing
 * @date 2019/9/6 13:23
 */
@Service
public class SubjectSummaryInfoServiceImpl implements ISubjectSummaryInfoService {
    private static final Logger logger = LoggerFactory.getLogger(SubjectSummaryInfoServiceImpl.class);
    @Resource
    private ExtGaSubjectSummaryInfoMapper extGaSubjectSummaryInfoMapper;

    @Override
    public SubjectSummaryInfoRes getModalByIdAndTenId(Long id, String tenantId) throws TxnException {
        if (id == null&& StringUtils.isBlank(tenantId)) {
            logger.error("科目总账主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaSubjectSummaryInfo subjectSummaryInfo = extGaSubjectSummaryInfoMapper.selectModalByIdAndTenId(id,tenantId);
        if (null == subjectSummaryInfo) {
            logger.error("根据主键[{}]查询的科目总账集合为空", id);
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(subjectSummaryInfo, SubjectSummaryInfoRes.class);
    }

    @Override
    public TxnPage<SubjectSummaryInfoRes> getPage(Integer pageNum, Integer pageSize,String tenantId,String beginDate,String endDate,String subjectId) throws TxnException {
        Page<GaSubjectSummaryInfo> page = PageHelper.startPage(pageNum, pageSize);
        if(StringUtils.isNotBlank(beginDate)){
             beginDate= beginDate.replace("-","");
        }
        if(StringUtils.isNotBlank(endDate)){
            endDate=endDate.replace("-","");
        }
        List<GaSubjectSummaryInfo> subjectSummaryInfos = extGaSubjectSummaryInfoMapper.selectAllByPageAndTenId(tenantId,beginDate,endDate,subjectId);
        if (subjectSummaryInfos.isEmpty()) {
            logger.error("查询的科目总账列表为空");
            throw new TxnException(TxnRespCode.SUCCESS.getCode(), "查询科目总账信息没有数据");
        }
        List<SubjectSummaryInfoRes> res=new ArrayList<>();
        for (GaSubjectSummaryInfo subjectSummaryInfo:subjectSummaryInfos) {
            SubjectSummaryInfoRes subjectSummaryInfoRes=new SubjectSummaryInfoRes();
            subjectSummaryInfoRes.setId(subjectSummaryInfo.getId());
            subjectSummaryInfoRes.setTenantId(subjectSummaryInfo.getTenantId());
            subjectSummaryInfoRes.setSubjectId(subjectSummaryInfo.getSubjectId());
            StringBuilder sb=new StringBuilder(subjectSummaryInfo.getSummaryMonth());
            sb.insert(4,"-");
            subjectSummaryInfoRes.setSummaryMonth(sb.toString());
            subjectSummaryInfoRes.setOrganizationId(subjectSummaryInfo.getOrganizationId());
            subjectSummaryInfoRes.setLastMonthCreditBal(subjectSummaryInfo.getLastMonthCreditBal());
            subjectSummaryInfoRes.setLastMonthDebitBal(subjectSummaryInfo.getLastMonthDebitBal());
            subjectSummaryInfoRes.setDebitAmount(subjectSummaryInfo.getDebitAmount());
            subjectSummaryInfoRes.setDebitBal(subjectSummaryInfo.getDebitBal());
            subjectSummaryInfoRes.setDebitCount(subjectSummaryInfo.getDebitCount());
            subjectSummaryInfoRes.setCreditAmount(subjectSummaryInfo.getCreditAmount());
            subjectSummaryInfoRes.setCreditBal(subjectSummaryInfo.getCreditBal());
            subjectSummaryInfoRes.setCreditCount(subjectSummaryInfo.getCreditCount());
            subjectSummaryInfoRes.setUpdateBy(subjectSummaryInfo.getUpdateBy());
            subjectSummaryInfoRes.setUpdateTime(subjectSummaryInfo.getUpdateTime());
            subjectSummaryInfoRes.setCreateTime(subjectSummaryInfo.getCreateTime());
            res.add(subjectSummaryInfoRes);
        }
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }
}
